const http = require("http")
const queryString = require('querystring')
const mysql = require('mysql')

const server = http.createServer((req,res)=> {
    let postVal = "";
    req.on("data",(chunk)=> {
        postVal += chunk;
    })
    req.on("end",()=> {
        // res.write()
        // console.log(queryString.parse(postVal));

        let formVal = queryString.parse(postVal);
        let userName = formVal.userName;
        let userPwd = formVal.userPwd;

        const connection = mysql.createConnection({
            host: "localhost",
            user: 'root',
            password: '123456',
            database: 'demo_user',
            port: '3306'
        })

        connection.connect();
        let sql = 'select * from normal_user where userName=? and userPwd=?';
        connection.query(sql,[userName,userPwd], (err,results,fields)=>{
            if(err) throw err;
            // console.log(results);
            if(results.length > 0) {
                res.writeHead(200,{'Content-Type': 'text/html;charset=utf-8'})
                res.write(`登录成功!您的用户名是：${results[0].userName}，密码是：${results[0].userPwd}`)
            } else {
                res.writeHead(200,{'Content-Type': 'text/html;charset=utf8'})
                res.write("用户名或密码错误!")
            }
            res.end();
        })
        connection.end();

        
    })
})

server.listen(8088)